package threading;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Microsoft
 * Date: 2023-07-07
 * Time: 19:13
 */

//线程不安全
public class ThreadDemo13 {
    static class Counter {
        public int count = 0;
        synchronized void increase() {
            count++;
        }
    }
    public static void main(String[] args) throws InterruptedException {
        final Counter counter = new Counter();

        //搞两个线程，两个线程分别对这个counter加50000次
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });
        t1.start();
        t2.start();

        //等待两个线程执行结束，然后看结果
        t1.join();
        t2.join();
        System.out.println(counter.count);
    }
}
